/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.core.ui.dialogs;


import java.util.Iterator;

import org.eclipse.gef.commands.CommandStack;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.uml2.uml.BehavioredClassifier;
import org.eclipse.uml2.uml.Classifier;
import org.eclipse.uml2.uml.EncapsulatedClassifier;
import org.eclipse.uml2.uml.Operation;
import org.eclipse.uml2.uml.Port;

import com.cea.papyrus.core.PapyrusPlugin;
import com.cea.papyrus.core.commands.UMLElementCreateCommand;
import com.cea.papyrus.core.editor.CommandStackUtils;

/**
 * Dialog to select an Operation of a BehavioredClassifier.
 * 
 * @author David Servat
 * @since 5. apr. 2007
 */
public class BehavioredClassifierOperationTreeSelectionDialog extends Dialog{

	/**
	 * The BehavioredClassifier for which we want to show a list of operations.
	 */
	protected BehavioredClassifier behavioredClassifier;

	/**
	 * Chosen operation.
	 */
	protected Operation operation;

	/**
	 * The selection tree.
	 */
	protected Tree tree;

	/**
	 * Button to create new operation
	 */
	protected Button newButton;

	/**
	 * Label for description
	 */
	protected Label label;

	/**
	 * Constructor with parent shell and behavioredClassifier.
	 * 
	 * @param parent the Shell.
	 * @param behavioredClassifier the BehavioredClassifier.
	 */
	public BehavioredClassifierOperationTreeSelectionDialog(Shell parent, BehavioredClassifier behavioredClassifier){
		super(parent);
		// add the resize ability to the window
		setShellStyle(SWT.RESIZE | super.getShellStyle());
		this.behavioredClassifier = behavioredClassifier;
	}

	/**
	 * Creates the dialog area.
	 * 
	 * @param parent composite.
	 * 
	 * @return control Control.
	 */
	protected Control createDialogArea(Composite parent){
		Composite composite = (Composite)super.createDialogArea(parent);

		label = new Label(composite, SWT.NONE);
		label.setText("Select an existing operation or create a new one:");
		label.setLayoutData(new GridData());

		newButton = new Button(composite, SWT.NONE);
		newButton.setText("New Operation");
		newButton.setLayoutData(new GridData(200, 25));

		tree = new Tree(composite, SWT.SINGLE | SWT.BORDER | SWT.V_SCROLL);

		final BehavioredClassifier currentClassifier = behavioredClassifier;

		//build subtree for operations contained in the BehavioredClassifier
		TreeItem item = new TreeItem(tree, SWT.NONE);
		item.setText("Operations of the classifier:");
		item.setImage(PapyrusPlugin.UML_LABEL_PROVIDER.getImage(currentClassifier));
		buildTreeList(item, currentClassifier);

		final TreeItem classifierItem = item;

		//build subtree for operations contained in all behavioral ports
		if(behavioredClassifier instanceof EncapsulatedClassifier){
			Iterator iter = ((EncapsulatedClassifier)behavioredClassifier).getOwnedPorts().iterator();
			while(iter.hasNext()){
				Port port = (Port)iter.next();
				if(port.isBehavior()){
					item = new TreeItem(tree, SWT.NONE);
					item.setText("Operations of the behavioral port: "+port.getName());
					item.setImage(PapyrusPlugin.UML_LABEL_PROVIDER.getImage(port));
					buildTreeList(item, (Classifier)port.getType());
				}
			}
		}
		tree.setLayoutData(new GridData(550, 300));

		tree.addSelectionListener(new SelectionListener() {
			public void widgetSelected(SelectionEvent e){
				TreeItem[] selection = tree.getSelection();
				if(selection[0].getData() instanceof Operation){
					operation = (Operation)selection[0].getData();
				}
			}

			public void widgetDefaultSelected(SelectionEvent e) {
			}
		});

		newButton.addMouseListener(new MouseListener(){
			public void mouseDoubleClick(MouseEvent e) {
			}

			public void mouseDown(MouseEvent e) {
			}

			public void mouseUp(MouseEvent e) {

				// creation command
				final UMLElementCreateCommand command = new UMLElementCreateCommand(Operation.class, currentClassifier);

				CommandStack commandStack = CommandStackUtils.getCommandStack();
				if(commandStack!= null) {
					commandStack.execute(command);
				}

				Operation newOperation = (Operation)command.getElement();
				// add the new Operation
				TreeItem operationItem = new TreeItem(classifierItem, SWT.SINGLE);
				operationItem.setText(com.cea.papyrus.umlutils.Operation.getSignature(newOperation, true));
				operationItem.setData(newOperation);
				operationItem.setImage(PapyrusPlugin.UML_LABEL_PROVIDER.getImage(newOperation));

				tree.setSelection(operationItem);
				operation = (Operation)operationItem.getData();

			}

		});

		return composite;
	}

	/**
	 * This method builds an Operation tree widget for each Classifier
	 * 
	 * @param classifier Classifier 
	 * @param item TreeItem 
	 */
	protected void buildTreeList(TreeItem classifierTree, Classifier classifier){
		Iterator iter = classifier.getAllOperations().iterator();
		while(iter.hasNext()){
			Operation currentOperation = (Operation)iter.next();
			TreeItem item = new TreeItem(classifierTree, SWT.SINGLE);
			item.setText(com.cea.papyrus.umlutils.Operation.getSignature(currentOperation, true));
			item.setData(currentOperation);
			item.setImage(PapyrusPlugin.UML_LABEL_PROVIDER.getImage(currentOperation));
		}
	}


	/**
	 * Returns the associated Operation.
	 * 
	 * @return operation Operation
	 */
	public Operation getResult(){
		return operation;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell)
	 */
	@Override
	protected void configureShell(Shell shell) {
		super.configureShell(shell);
		shell.setText(behavioredClassifier.getName()+": Operation Selection");
	}

}
